package com.gitee.sys.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mybase.top.entity.BaseDict;
import com.gitee.sys.mapper.BaseDictMapper;
import com.gitee.sys.service.IBaseDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2018-12-10
 */
@Service
@Component
public class BaseDictServiceImpl extends ServiceImpl<BaseDictMapper, BaseDict> implements IBaseDictService {

    @Override
    public IPage<BaseDict> getPage(long current, long size, BaseDict baseDict) {
        IPage<BaseDict> page = new Page<>(current, size);
        QueryWrapper<BaseDict> sysUserQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(baseDict.getDictCode())){
            sysUserQueryWrapper.like("dict_code", baseDict.getDictCode());
        }
        if(StringUtils.isNotEmpty(baseDict.getDictName())){
            sysUserQueryWrapper.like("dict_name", baseDict.getDictName());
        }
        if(StringUtils.isNotEmpty(baseDict.getDictType())){
            sysUserQueryWrapper.eq("dict_type", baseDict.getDictType());
        }
        if(StringUtils.isNotEmpty(baseDict.getScene())){
            sysUserQueryWrapper.like("scene", baseDict.getScene());
        }

        page = this.page(page, sysUserQueryWrapper);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveDict(BaseDict baseDict) {
        baseDict.setCreateTime(LocalDateTime.now());
        baseDict.setIsLock(0);
        baseDict.setIsValid(1);
        return baseMapper.insert(baseDict);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDict(BaseDict baseDict) {
        UpdateWrapper<BaseDict> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("dict_code", baseDict.getDictCode());
        updateWrapper.set("dict_name", baseDict.getDictName());
        updateWrapper.set("dict_type", baseDict.getDictType());
        updateWrapper.set("scene", baseDict.getScene());
        updateWrapper.set("update_time", LocalDateTime.now());
        updateWrapper.eq("id", baseDict.getId());
        return baseMapper.update(baseDict, updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delDict(Long dictId) {
        QueryWrapper<BaseDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", dictId);
        return baseMapper.delete(queryWrapper);
    }
}
